<!--
 * @FileDescription:
 * @Author: cjw
 * @Date: 2022/3/8 20:44
 -->
<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :destroy-on-close="close_modal_destroy"
    :close-on-click-modal="close_modal"
    @close="handleCancel"
    width="500px"
  >
    <el-form ref="ruleForm" :model="addFrom" label-width="80px" :rules="rules">
      <el-form-item label="接口名称" prop="name" required>
        <el-input v-model="addFrom.name"></el-input>
      </el-form-item>
      <el-form-item label="请求类型" prop="method" required>
        <el-select v-model="addFrom.method" placeholder="请选择请求类型">
          <el-option label="添加" value="添加"></el-option>
          <el-option label="删除" value="删除"></el-option>
          <el-option label="修改" value="修改"></el-option>
          <el-option label="查询" value="查询"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属组" prop="team" required>
        <el-input v-model="addFrom.team"></el-input>
      </el-form-item>
      <el-form-item label="接口路径" prop="url" required>
        <el-input v-model="addFrom.url"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('addFrom')">立即发布</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {addController, detailController, updateController} from "@/api/resourceMange";

export default {
  name: "addoreditBackSecurity",
  data() {
    return {
      title: undefined,
      visible: false,
      close_modal_destroy: true,
      close_modal: false,
      addFrom: {
        name: undefined,
        url: undefined,
        team: undefined,
        method: undefined
      },
      rules: {
        name: [
          {required: true, message: '请输入接口名称', trigger: 'blur'}
        ],
        url: [
          {required: true, message: '请输入接口路径', trigger: 'blur'}
        ],
        team: [
          {required: true, message: '请输入所属组', trigger: 'blur'}
        ],
        method: [
          {required: true, message: '请输入请求类型', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    add() {
      this.title = '新增接口'
      this.visible = true
    },
    edit(id) {
      this.title = '修改接口'
      this.visible = true
      this.addFrom.id = id
      detailController(id).then(res => {
        this.addFrom = res.body.data
      })
    },
    close() {
      this.visible = false
      this.addFrom = {
        name: undefined,
        url: undefined,
        team: undefined,
        method: undefined
      }
    },
    handleCancel() {
      this.close()
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if(valid) {
          if (this.title === '新增接口')
            addController(this.addFrom).then(res => {
              if (res.body.data) {
                this.$message.success("添加成功")
                this.$emit('ok')
                this.close()
              }
              else
                this.$message.error("请重新添加")
            })
          else {
            updateController(this.addFrom).then(res => {
              if (res.body.data) {
                this.$message.success("修改成功")
                this.$emit('ok')
                this.close()
              }
              else
                this.$message.error("请重新修改")
            })
          }
        } else {
          this.$message.error("请输入必选项");
          return false;
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
